<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class StaffProfile extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('memberlogin/requiredmessage','refresh');
	    }
	    elseif ($_SESSION['role_id'] !=1){
    		redirect('memberlogin/restriction','refresh');
	    }
	    $data['menus'] = 'menus';
	    //$data['left'] = 'hr/staffprofile_left';
		$this->load->vars($data);
    }

/** Staff List */
	public function index()
	{
		$data['title'] = "Staff profile - Friends-International Organization";
		$data['main'] = 'hr/staffprofile';
        $data['teams'] = $this->MTeam->getTeamDropdown();
		$data['profiles'] = $this->MStaff->getAllProfiles('');
		$this->load->vars($data);
		$this->load->view('template'); 
	}
    
    public function search()
    {
        if($this->input->post('searchprofile') || $this->input->post('TeamID') > 0){ 
            $data['title'] = "Staff profile - Friends-International Organization";
    		$data['main'] = 'hr/staffprofile';
            $data['teams'] = $this->MTeam->getTeamDropdown();
    		$data['profiles'] = $this->MStaff->getAllProfiles($this->input->post('searchprofile'), $this->input->post('TeamID'));
    		$this->load->vars($data);
    		$this->load->view('template');   
        }else {
           redirect('staffprofile','refresh'); 
        }
    }
    
	public function details($id)
	{
		$data['title'] = "Staff Details View";
		$data['main'] = 'hr/staffdetail';
        $user = $this->MStaff->getStaffDetailById($id);
		$data['user'] = $user;	
        $data['endprobation'] = $user['EndProbation'];
        $data['documents'] = $this->MStaff->getStaffDocumentByStaffId($id);
        $data['contracts'] = $this->MStaff->getStaffContractByStaffId($id);
        $data['checklist'] = $this->MStaff->getStaffCheckListByStaffId($id);
        $data['form'] = $this->MFormGlobal->getFormEditCancel('/index.php/staffprofile/editstaff/'.$id, '/index.php/staffprofile');
		$this->load->vars($data);
		$this->load->view('template');      
	}
    
    public function viewcontract($staffId, $staffContractId)
	{
		$data['title'] = "Staff Contract View";
		$data['main'] = 'hr/contractdetail';
        $data['contract'] = $this->MStaff->getStaffContractByStaffIdContractId($staffId,$staffContractId);
        $data['salaries'] = $this->MContract->getSalaryIncreaseByContractId($staffContractId);
        $data['appraisals'] = $this->MContract->getAppraisalByContractId($staffContractId);
        $data['amendments'] = $this->MContract->getAmendmentByContractId($staffContractId); 
        $data['form'] = $this->MFormGlobal->getFormCancel('/index.php/staffprofile/details/'.$staffId.'#tabs-3');
		$this->load->vars($data);
		$this->load->view('template');      
	}
	
	public function createStaff()
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->addStaff();	
	  		$this->session->set_flashdata('message','Staff created');
			redirect('staffprofile','refresh');
	  	}else{
			$data['title'] = "Add new Staff";
			$data['main'] = 'hr/staffadd';
			$data['roles'] = $this->MUser->getRoles();
			$data['status'] = $this->MStaff->getStatusDropdown();
			$data['positions'] = $this->MPosition->getPositionDropdown();	
			$data['teams'] = $this->MTeam->getTeamDropdown();
            $data['locations'] = $this->MStaff->getLocationDropDown();
            $data['lastSID'] = $this->MStaff->getLastSID(0,1);
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/staffprofile');
			$this->load->vars($data);
			$this->load->view('template');      
		}
	}
	
	public function editStaff($id=0)
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->updateStaff();	
	  		$this->session->set_flashdata('message','Staff updated');
			redirect('staffprofile','refresh');
	  	}else{
			$data['title'] = "Edit Staff";
			$data['main'] = 'hr/staffedit';
			$data['roles'] = $this->MUser->getRoles();
			$data['status'] = $this->MStaff->getStatusDropdown();
			$data['positions'] = $this->MPosition->getPositionDropdown();	
			$data['teams'] = $this->MTeam->getTeamDropdown();
            $data['locations'] = $this->MStaff->getLocationDropDown();
			$data['staff'] = $this->MStaff->getStaffDetailById($id);	
            $data['lastSID'] = $this->MStaff->getLastSID(0,1);
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/staffprofile');
			$this->load->vars($data);
			$this->load->view('template');       
		}
	}	
/** End Staff */
/** Staff Document */
	function createStaffDocument($id)
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->createStaffDocument($id);	
	  		$this->session->set_flashdata('message','Document created');
			redirect('staffprofile/details/'.$id.'#tabs-2','refresh');
	  	}else{
			$data['title'] = "Add Staff Document";
			$data['main'] = 'hr/staffdocumentadd';
            $data['staffId'] = $id;
			$data['documenttypes'] = $this->MStaff->getDocumentTypeDropDown();	
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/staffprofile/details/'.$id.'#tabs-2');
			$this->load->vars($data);
			$this->load->view('template');  
	  	}  
	}
	
	function editStaffDocument($staffId, $staffDocId)
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->updateStaffDocument($staffId, $staffDocId);
	  		$this->session->set_flashdata('message','Document updated');
			redirect('staffprofile/details/'.$staffId.'#tabs-2','refresh');
	  	}else{
			$data['title'] = "Edit Staff Document";
			$data['main'] = 'hr/staffdocumentedit';
			$data['documenttypes'] = $this->MStaff->getDocumentTypeDropDown();
            $data['form'] = $this->MFormGlobal->getFormEditCancelBottom('/index.php/staffprofile/details/'.$staffId.'#tabs-2');
            $data['document'] = $this->MStaff->getStaffDocumentByStaffIdDocId($staffId, $staffDocId);	
            $data['staffId'] = $staffId;
            $data['staffDocId'] = $staffDocId;
			$this->load->vars($data);
			$this->load->view('template');    
	  	}
	}
/** End Staff Docuemtn */
	
/** Staff Contract */
	function createStaffContract($id)
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->createStaffContract($id);	
	  		$this->session->set_flashdata('message','Contract created');
			redirect('staffprofile/details/'.$id.'#tabs-3','refresh');
	  	}else{
			$data['title'] = "Add Staff Contract";
			$data['main'] = 'hr/staffcontractadd';
            $data['staffId'] = $id;
			$data['positions'] = $this->MPosition->getPositionDropdown();	
			$data['teams'] = $this->MTeam->getTeamDropdown();
            $data['contracttypes'] = $this->MStaff->getContractTypeDropDown();
            $data['worktypes'] = $this->MStaff->getWorkTypeDropDown(); 
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/staffprofile/details/'.$id.'#tabs-3');
			$this->load->vars($data);
			$this->load->view('template');  
	  	}  
	}
	function deleteStaffContract($staffId, $staffContractId){
		$this->MStaff->deleteStaffContract($staffId,$staffContractId);
		$this->session->set_flashdata('message','Contract deleted');
		redirect('staffprofile/details/'.$staffId.'#tabs-3','refresh');
	}
	
/** End Staff Contract */
	
/** Staff Check list */
    function createStaffCheckList($id)
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->createStaffCheckList($id);	
	  		$this->session->set_flashdata('message','Checklist created');
			redirect('staffprofile/details/'.$id.'#tabs-4','refresh');
	  	}else{
			$data['title'] = "Add Staff CheckList";
			$data['main'] = 'hr/staffchecklistadd';
            $data['staffId'] = $id;
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/staffprofile/details/'.$id.'#tabs-4');
			$this->load->vars($data);
			$this->load->view('template');  
	  	}  
	}
    function editStaffCheckList($id)
	{
		if ($this->input->post('submit')){
	  		$this->MStaff->updateStaffCheckListByStaffId($id);
	  		$this->session->set_flashdata('message','Checklist updated');
			redirect('staffprofile/details/'.$id.'#tabs-4','refresh');
	  	}else{
			$data['title'] = "Edit Staff CheckList";
			$data['main'] = 'hr/staffchecklistedit';
            $data['staffId'] = $id;
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/staffprofile/details/'.$id.'#tabs-4');
            $data['checklist'] = $this->MStaff->getSingleStaffCheckListByStaffId($id);
			$this->load->vars($data);
			$this->load->view('template');  
	  	}  
	}
/** End Staff check list */
	
	function delete($id){
		$this->MUser->deleteUser($id);
		$this->session->set_flashdata('message','User deleted');
		redirect('staffprofile','refresh');
	}
}
